#include "TaskList.h"

TaskList::TaskList(){

}

LinkedList<Task> TaskList::getTask(){
	return Tasks;
}

int TaskList::getLength(){
	return Tasks.getLength();
}

void TaskList::add(Task newTask){
	Tasks.insert(1,newTask);
}

void TaskList::remove(int taskNum){
	Tasks.remove(taskNum);
}

void TaskList::retrieve(int taskNum,Task& taskItem){
	Tasks.retrieve(taskNum,taskItem);
}

void TaskList::clear(){
	Tasks.clear();
}

void TaskList::display(){
	int n;
	string toPrint;
	n=Tasks.getLength();
	for(int i=1;i<=n;i++){
		Task taskToPrint;
		Tasks.retrieve(i,taskToPrint);
		toPrint=taskToPrint.toPrint();
		cout<<toPrint<<endl;
	}
}
void TaskList::search(string searchStr,TaskList& searchResult){

	int curSize;
	curSize=Tasks.getLength();
	string segmentForSearch;
	istringstream searchStream(searchStr);
	for(int i=1;i<=curSize;i++){
		bool isFound=false;
		Task curTask;
		Tasks.retrieve(i,curTask);
		while(searchStream>>segmentForSearch){
			if(stringContains(curTask.getContent(),segmentForSearch)){
				isFound=true;
			}
		}
		if(isFound){
			searchResult.add(curTask);
		}
	}
}

int TaskList::searchTask(Task searchedTask){
	Task curTask;
	int pos=0;
	for(int i=1;i<=Tasks.getLength();i++){
		Tasks.retrieve(i,curTask);
		if(curTask.getContent()==searchedTask.getContent() && curTask.getStartTime().equal(searchedTask.getStartTime())
			&&curTask.getEndTime().equal(searchedTask.getEndTime())){
		    pos=i;
		}
	}
	return pos;
}

bool TaskList::stringContains(const string inputStr, const string searchStr)
{
	bool isFound;
	size_t contains;

	contains = inputStr.find(searchStr);

	if(contains != string::npos){
		isFound=true;
	}
	else{
		isFound=false;
	}return isFound;
}



void TaskList::sortByStartTimeAsc()
{

	for(int i=Tasks.getLength();i>1;i--)
	{
		for(int j=1;j<i;j++)
		{
			Task t1;
			Task t2;
			Tasks.retrieve(j,t1);
			Tasks.retrieve(j+1,t2);
			Time time1 = t1.getStartTime();
			Time time2 = t2.getStartTime();

			if( time1.largerThan( time2 ) )
			{
				Tasks.remove(j);
				Tasks.insert(j+1,t1);
			}

		}
	}
}

void TaskList::sortByStartTimeDesc()
{

	for(int i=Tasks.getLength();i>1;i--)
	{
		for(int j=1;j<i;j++)
		{
			Task t1;
			Task t2;
			Tasks.retrieve(j,t1);
			Tasks.retrieve(j+1,t2);
			Time time1 = t1.getStartTime();
			Time time2 = t2.getStartTime();

			if( time1.lessThan( time2 ) )
			{
				Tasks.remove(j);
				Tasks.insert(j+1,t1);
			}

		}
	}
}

void TaskList::sortByEndTimeDesc()
{

	for(int i=Tasks.getLength();i>1;i--)
	{
		for(int j=1;j<i;j++)
		{
			Task t1;
			Task t2;
			Tasks.retrieve(j,t1);
			Tasks.retrieve(j+1,t2);
			Time time1 = t1.getEndTime();
			Time time2 = t2.getEndTime();

			if( time1.lessThan( time2 ) )
			{
				Tasks.remove(j);
				Tasks.insert(j+1,t1);
			}

		}
	}
}

void TaskList::sortByEndTimeAsc()
{

	for(int i=Tasks.getLength();i>1;i--)
	{
		for(int j=1;j<i;j++)
		{
			Task t1;
			Task t2;
			Tasks.retrieve(j,t1);
			Tasks.retrieve(j+1,t2);
			Time time1 = t1.getEndTime();
			Time time2 = t2.getEndTime();

			if( time1.largerThan( time2 ) )
			{
				Tasks.remove(j);
				Tasks.insert(j+1,t1);
			}

		}
	}
}

void TaskList::sortByAlphabetAsc()
{
	for(int i=Tasks.getLength();i>1;i--)
	{
		for(int j=1;j<i;j++)
		{
			Task t1;
			Task t2;
			Tasks.retrieve(j,t1);
			Tasks.retrieve(j+1,t2);
			string content1 = t1.getContent();
			string content2 = t2.getContent();
			if( content1>content2 )
			{
				Tasks.remove(j);
				Tasks.insert(j+1,t1);
			}

		}
	}
}

void TaskList::sortByAlphabetDesc()
{
	for(int i=Tasks.getLength();i>1;i--)
	{
		for(int j=1;j<i;j++)
		{
			Task t1;
			Task t2;
			Tasks.retrieve(j,t1);
			Tasks.retrieve(j+1,t2);
			string content1 = t1.getContent();
			string content2 = t2.getContent();
			if( content1<content2 )
			{
				Tasks.remove(j);
				Tasks.insert(j+1,t1);
			}
		}
	}
}




